<template>
    <div class="br-detail" v-loading="loading">
        <div class="sp-top">
            <el-form label-width="90px" size="mini" style="width: 50%" class="demo-form-inline">
                <el-form-item label="模板名称">
                    <el-input name="模板名称" v-model="specialTemplate.branchTemplate.templateName"></el-input>
                </el-form-item>
                <el-form-item label-width="0">
                    <el-col :span="12">
                        <el-form-item label="模板编号">
                            <el-input placeholder="自动生成" disabled v-model="specialTemplate.branchTemplate.templateCode"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="风险类型:">
                            <el-select name="entryType" v-model="specialTemplate.branchTemplate.entryType" style="width: 100%" @change="changSetting">
                                <el-option
                                    v-for="item in entryTypeList"
                                    :key="item.dictKey"
                                    :value="item.dictKey"
                                    :label="item.dictKey + ' ' + item.dictKeyName"
                                    :name="'entryType-' + item.dictKey"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <!-- <el-input v-model="specialTemplate.specialCalc.entryType"></el-input> -->
            </el-form>
        </div>
        <div class="sp-itme">
            <ItemBranchSetting0
                style="height: 380px"
                @set-branch-data="getBranchData"
                :entryTypeData="specialTemplate.branchTemplate.entryType"
                :riskEntryBranchList="riskEntryBranchList"
            ></ItemBranchSetting0>
        </div>
        <div class="sp-bout">
            <div class="sp-but-div">
                <el-button @click="openBranch()">取消</el-button>
                <el-button
                    type="primary"
                    name="确定"
                    @click="submitSpecial"
                    :disabled="loading"
                    v-show="itemData.type == 'edit-branch' || itemData.type == 'copy-branch' || itemData.type == 'add-branch'"
                    >确定</el-button
                >
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import DictionConstUtil, { SGU_CACHE } from '@/utils/diction-const-util';
import { mixins } from 'vue-class-component';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import ItemBranchSetting0 from './item-branch-setting0.vue';

@Component({
    name: 'ItemBranchTemplateDetail',
    components: {
        ItemBranchSetting0,
    },
})
export default class ItemBranchTemplateDetail extends mixins(RiskEmitter) {
    @Prop() itemData: any;
    specialTemplate: any = {
        branchTemplate: {
            templateCode: '自动生成',
            templateName: '',
            entryType: '0',
        },
        riskEntryBranchs: [],
    };
    riskEntryBranchList: any = [];
    entryTypeList: any[] = [];
    loading: boolean = false;

    async mounted() {
        // 风险类型
        this.entryTypeList = DictionConstUtil.PojoToList(DictionConstUtil.EntryType);
        this.getspecicl();
    }

    changSetting() {
        let val = this.specialTemplate.branchTemplate.entryType;
        this.broadcast('ItemBranchSetting0', 'setentryTypes', val);
    }

    // 获取组件数据
    getBranchData(val: any) {
        this.specialTemplate.riskEntryBranchs = val;
    }

    // 查看编辑数据
    getspecicl() {
        let url;
        if (this.itemData.type == 'check-branch') {
            url = '/entry/template/check/query/';
        } else {
            url = '/entry/template/branch/query/';
        }
        if (this.itemData.content) {
            this.loading = true;
            this.$axios.get(url + this.itemData.content).then((res: any) => {
                this.specialTemplate.branchTemplate = res.branchTemplate;
                this.specialTemplate.riskEntryBranchs = res.riskEntryBranchs;
                this.riskEntryBranchList = res.riskEntryBranchs;
                setTimeout(() => {
                    this.loading = false;
                }, 2000);
            });
        } else {
            return;
        }
    }

    // 关闭
    openBranch() {
        this.$emit('close-custom', this.itemData);
    }

    submitSpecial() {
        if (this.specialTemplate.branchTemplate.templateName == '') {
            this.yh_message_error('模板名称不能为空');
            return;
        }
        if (this.specialTemplate.riskEntryBranchs.length == 0) {
            this.yh_message_error('需要至少设置一个分支');
            return;
        }
        let url;
        this.loading = true;
        if (this.itemData.type == 'edit-branch') {
            url = '/entry/template/branch/update';
        } else if (this.itemData.type == 'add-branch' || this.itemData.type == 'copy-branch') {
            url = '/entry/template/branch/insert';
        }
        this.$axios
            .post(url, this.specialTemplate)
            .then(res => {
                this.loading = false;
                this.yh_message_success('操作成功');
                this.openBranch();
                DictionConstUtil.FNBranchTemplateList();
            })
            .catch(() => {
                this.loading = false;
            });
    }
}
</script>
<style lang="scss" scoped>
.br-detail {
    color: #222;
    padding: 10px;
    overflow-x: scroll;

    .sp-top {
        // width: calc(100vw - 20px);
        width: 100%;
        border: 1px solid #eee;
        padding: 10px 0;
        min-width: 1000px;
        height: 100px;
        margin-bottom: 10px;

        & /deep/ .el-form-item--mini .el-form-item__label {
            color: #222;
        }
    }

    .sp-itme {
        min-width: 1000px;
    }

    .sp-bout {
        border: 1px solid #eee;
        padding: 10px;
        height: 55px;
        min-width: 1000px;

        .sp-but-div {
            float: right;
        }
    }
}
</style>
